﻿// ***********************************************************************
// Assembly         : NIMServerApi
// Author           : Walter
// Created          : 11-15-2016
//
// Last Modified By : Walter
// Last Modified On : 11-15-2016
// ***********************************************************************
// <copyright file="User.cs" company="51iface">
//     Copyright 51iface©  2016
// </copyright>
// <summary></summary>
// ***********************************************************************

using System;
using System.Configuration;
using NIMServerApi.Commons;
using NIMServerApi.Messages;

namespace NIMServerApi.Methods
{
    /// <summary>
    /// 云信ID&用户名片封装类.
    /// </summary>
    public class UserMethod:BaseMethod
    {
        public UserMethod(string appKey, string appSecret):base(appKey,appSecret)
        {
        }
        /// <summary>
        /// 在云信中创建账户.
        /// </summary>
        /// <param name="accid">云信ID，最大长度32字符，必须保证一个.</param>
        /// <param name="name">云信ID昵称，最大长度64字符，用来PUSH推送时显示的昵称.</param>
        /// <param name="props">json属性，第三方可选填，最大长度1024字符.</param>
        /// <param name="icon">云信ID头像URL，第三方可选填，最大长度1024.</param>
        /// <param name="token">云信ID可以指定登录token值，最大长度128字符.如果未指定，会自动生成token，并在创建成功后返回</param>
        /// <returns>UserMessage.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// Paramer 'accid' is required.
        /// or
        /// Paramer 'name' is required.
        /// </exception>
        public UserMessage Create(string accid,string name,string props="",string icon="",string token="")
        {
            if (string.IsNullOrEmpty(accid))
            {
                throw new ArgumentNullException("Paramer 'accid' is required.");
            }
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("Paramer 'name' is required.");
            }
            string postStr = $"accid={accid}&name={name}";
            if (!string.IsNullOrEmpty(props))
            {
                postStr += $"&props={props}";
            }
            if (!string.IsNullOrEmpty(icon))
            {
                postStr += $"&icon={icon}";
            }
            if (!string.IsNullOrEmpty(token))
            {
                postStr += $"&token={token}";
            }
            string json = NIMHttpClient.ExecutePost(appKey, appSecret, NIMCloud.NIMURI + "/user/create.action",postStr,"");
            System.Diagnostics.Debug.WriteLine(json);
            return json.JsonToObject<UserMessage>();
        }

        /// <summary>
        /// 云信ID基本信息更新.
        /// </summary>
        /// <param name="accid">云信ID，最大长度32字符，必须保证一个.</param>
        /// <param name="props">json属性，第三方可选填，最大长度1024字符.</param>
        /// <param name="token">云信ID可以指定登录token值，最大长度128字符.</param>
        /// <returns>UserMessage.</returns>
        /// <exception cref="System.ArgumentNullException">Paramer 'accid' is required.</exception>
        public UserMessage RefreshToken(string accid, string props, string token)
        {
            if (string.IsNullOrEmpty(accid))
            {
                throw new ArgumentNullException("Paramer 'accid' is required.");
            }
            string postStr = $"accid={accid}";
            string json = NIMHttpClient.ExecutePost(appKey, appSecret, NIMCloud.NIMURI + "/user/refreshToken.action", postStr, "");
            System.Diagnostics.Debug.WriteLine(json);
            return json.JsonToObject<UserMessage>();
        }

        /// <summary>
        /// 封禁云信ID.
        /// </summary>
        /// <param name="accid">云信ID，最大长度32字符，必须保证一个APP内唯一.</param>
        /// <param name="needkick">是否踢掉被禁用户，true或false，默认false，if set to <c>true</c> [踢掉].</param>
        /// <returns>System.Int32.</returns>
        /// <exception cref="System.ArgumentNullException">Paramer 'accid' is required.</exception>
        public int Block(string accid, bool needkick = false)
        {
            if (string.IsNullOrEmpty(accid))
            {
                throw new ArgumentNullException("Paramer 'accid' is required.");
            }
            string postStr = $"accid={accid}&needkick={needkick}";
            string json = NIMHttpClient.ExecutePost(appKey, appSecret, NIMCloud.NIMURI + "/user/block.action", postStr, "");
            return base.TryGetJsonValue<int>(json,"code");
        }

        /// <summary>
        /// 解禁云信ID.
        /// </summary>
        /// <param name="accid">The accid.</param>
        /// <returns>System.Int32.</returns>
        /// <exception cref="System.ArgumentNullException">Paramer 'accid' is required.</exception>
        public int Unblock(string accid)
        {
            if (string.IsNullOrEmpty(accid))
            {
                throw new ArgumentNullException("Paramer 'accid' is required.");
            }
            string postStr = $"accid={accid}";
            string json = NIMHttpClient.ExecutePost(appKey, appSecret, NIMCloud.NIMURI + "/user/unblock.action", postStr, "");
            return base.TryGetJsonValue<int>(json, "code");
        }


    }
}